<%@page import="com.wanmait.movies.vo.Movie"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.wanmait.movies.vo.Maneger"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<% Maneger m =(Maneger)session.getAttribute("maneger");
	if(m==null){
	response.sendRedirect(request.getContextPath()+"/manegerlog.jsp");
	}%>

<style type="text/css">
	#moviecircle{
	width:500px;
	
	padding:100px 50px;
	}
	#movielist{
	width:808px;
	height:410px;
	margin-top:10px;
	border:1px solid #ddd;
	border-radius: 10px;
	overflow: auto;
	}
	.everymovie{
	width:160px;
	margin-left:40px;
	height:130px;
	text-align:center;
	border:1px solid #ddd;
	border-radius: 10px;
	float:left;
	}
	body{
	background-color: white;
	font-size:15px;
	font-weight: bold;
	}
	.everymovie img{
	width: 150px;
	height:90px;
	}
	#movie{
	border:2px solid gray;
	border-radius: 10px;
	margin-left:10px;
	}
	#findinput{
	background-color: white;
	border-radius: 5px;
	margin-left:10px;
	}
	#fenye{
	width:500px;
	height:30px;
	align-content: center;
	padding-left: 300px;
	}
	#ThisPage{
	width:30px;
	}
</style>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
	$(function(){
		$("#findinput").click(function(){
			var movie = $("#movie").val();
			$("#fenye").css("display","none");
			$.ajax({
				url:"/movies/ManegerServlet?action=findMovie",
				async:false,
				type:"get",
				data:"movie="+movie,
				success:function(s){
					if(s=="0"){
						alert("没有该电影");	
					}else{
						
						var list =JSON.parse(s);
						$("#movielist").children().remove();
						for(var i =0;i<list.length;i++){
							var op =$("<div><div>");
							op.attr("mid",list[i].id);
							op.addClass("everymovie");
							var op1 =$("<div><div>");
							
							var img =$("<img src='<%=request.getContextPath()%>/images/"+list[i].movieImage+"'>");
							op1.append(img);
							var op2 =$("<div>"+list[i].movieName+"<div>");
					
							op.append(op1);
							op.append(op2);
							
							$("#movielist").append(op);
						}
				   }	
					
				}
			});
	
		});	
		
	});
	
	$(function(){
		$("#movielist").on("click",".everymovie",function(){
			var movie = $(this).attr("mid");
			var thispage=$("#ThisPage").val();
			$.ajax({
				url:"/movies/ManegerServlet?action=deleteMovie",
				async:false,
				type:"get",
				data:"movie="+movie+"&thispage="+thispage,
				success:function(s){
					alert("删除成功");
					$("#movielist").html(s);
					$("#ThisPage").val(thispage);
				}
			});
			
		});
	});
	$(function () {
		$("#LastPage").click(function(){//上一页
			//alert("11654");
			var thispage=$("#ThisPage").val();
			thispage--;
			if(thispage==""){
				thispage=1;
			}
			if(thispage>0){
				$.ajax({
					url:"/movies/ManegerServlet?action=pagedeletemovie",
					async:false,
					type:"get",
					data:"thispage="+thispage,
					success:function(s){
						$("#movielist").html(s);
						$("#ThisPage").val(thispage);
					}
				})
			}
		});
	});
	$(function () {
		$("#NextPage").click(function(){//下一页
			var thispage=$("#ThisPage").val();
			var pagemax=$("#NextPage").attr("pagemax");
			      thispage++;
			      //alert(pagemax);
			      if(thispage==""){
						thispage=1;
					}
			if(thispage<=pagemax){
				$.ajax({
					url:"/movies/ManegerServlet?action=pagedeletemovie",
					async:false,
					type:"get",
					data:"thispage="+thispage,
					success:function(s){
						$("#movielist").html(s);
						$("#ThisPage").val(thispage);
					}
				})
			}
			});
	});
	
	$(function () {//回车事件
		$('#ThisPage').keydown(function(e){
			var thispage=$("#ThisPage").val();
			var pagemax=$("#NextPage").attr("pagemax");
			if(thispage==""){
				thispage=1;
			}
			var h=e||window.event;
			if(h.which=="13"){
				if(thispage<=pagemax){
					$.ajax({
						url:"/movies/ManegerServlet?action=pagedeletemovie",
						async:false,
						type:"get",
						data:"thispage="+thispage,
						success:function(s){
							$("#movielist").html(s);
							$("#ThisPage").val(thispage);
						}	
					})
				}
			}
			});
	});
	$(function () {
		$("#ThisPage").blur(function(){//这是text查找页数的
			var thispage=$("#ThisPage").val();
			var pagemax=$("#NextPage").attr("pagemax");
			if(thispage==""){
				thispage=1;
			}
			if(thispage<=pagemax){
				$.ajax({
					url:"/movies/ManegerServlet?action=pagedeletemovie",
					async:false,
					type:"get",
					data:"thispage="+thispage,
					success:function(s){
						$("#movielist").html(s);
						$("#ThisPage").val(thispage);
					}
				})
			}
			
		});
	});
	
	$(function () {
		$("#movie").focus(function(){
		     	$("#movie").val("");
				$("#fenye").css("display","block");
				$.ajax({
					url:"/movies/ManegerServlet?action=pagedeletemovie",
					async:false,
					type:"get",
					data:"thispage=1",
					success:function(s){
						$("#movielist").html(s);
						$("#ThisPage").val(1);
					}
				})

		});
	});
</script>

</head>
<body>
      <div id="moviecircle">
            <div>电影名:<input type="text" id="movie"><input type="button" value="搜索" id="findinput"></div>    
            <div id="movielist">
            	<%
            	ArrayList<Movie>  movieList  = (ArrayList<Movie>)request.getAttribute("movieList");
            	for(Movie movie:movieList){%>
            		<div mid="<%=movie.getId()%>" class="everymovie"> 
            			<div><img src="<%=request.getContextPath()%>/images/<%=movie.getMovieImage() %>"></div>
            			<div><%=movie.getMovieName() %></div>
            		</div>
            	<%}
            	%>
            </div>
      	<div id="fenye"><!-- 分页查询 -->
          <a href="javascript:" id="LastPage">上一页</a>
          <input id="ThisPage"  type="text"  value="<%=request.getAttribute("thispage") %>" />
          <a href="javascript:" id="NextPage" pagemax="<%=request.getAttribute("pagemax")%>">下一页</a>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;共(<%=request.getAttribute("pagemax") %>)页
       </div>
      </div>
</body>
</html>